<?php

namespace App\Http\Controllers\Admin\Cashier;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cashier\Category;

class CategoryController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_cashier' => 'active open',
            '_category' => 'active',
            'categories' => Category::get_categories()
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //第一种方法
//        $categories = Category::where("parent_id", 0)->orderBy('sort_order', 'desc')->get();
//        foreach ($categories as $category){
//            $category['children'] = Category::where('parent_id', $category['id'])->orderBy('sort_order', 'desc')->get();
//        }


//        return $categories;

        return view('admin.cashier.category.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.cashier.category.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:categories|max:255',
            'sort_order' => 'required|integer|max:99',
            'description' => 'required'
        ]);
        Category::create($request->all());
        Category::clear();
        return redirect(route('cashier.category.index'))->with('success', '新增分类成功~');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $category = Category::find($id);
        return view('admin.cashier.category.edit', compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'sort_order' => 'required|integer|max:99',
            'description' => 'required'
        ]);
        $category = Category::find($id);
        $category->update($request->all());
        Category::clear();
        return redirect(route('cashier.category.index'))->with('success', '编辑分类成功~');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //第一种方法(插件)
//        if (!Category::check_children($id)) {
//            return back()->with('error', '此分类下有二级分类，不能删除~');
//        }
//
//        Category::destroy($id);
//        Category::clear();
//        return back()->with('success', '删除分类成功~');

        //第二种方法(ajax)
        if (!Category::check_children($id)) {
            return response()->json([
                'status' => 0,
                'msg' => '此分类下有二级分类不能删除'
            ]);
        }
        Category::destroy($id);
        Category::clear();
        return response()->json([
            'status' => 1,
            'msg' => '删除分类成功'
        ]);
    }

    /** 改变属性
     * @param Request $request
     */
    public function change_attr(Request $request)
    {
        $category = Category::find($request->id);
        $attr = $request->attr;
        $category->$attr = !$category->$attr;
        $category->save();
        Category::clear();

    }


    /** ajax排序
     * @param Request $request
     */
    public function sort_order(Request $request)
    {
        $category = Category::find($request->id);
        $category->sort_order = $request->sort_order;
        $category->save();
        Category::clear();
    }
}
